package com.liyk.shop.utils;

import com.liyk.shop.dto.MailMsgDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @program: springcloud-shop
 * @description:
 * @author: lyk
 * @create: 2019-12-13 15:41
 **/
@Slf4j
@Service
public class MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 简单邮件
     */
    public void sendSimpleMail(MailMsgDto dto) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(dto.getFrom());
        simpleMailMessage.setTo(dto.getTo());
        simpleMailMessage.setSubject(dto.getSubject());
        simpleMailMessage.setText(dto.getContent());
        javaMailSender.send(simpleMailMessage);//发送
    }

    /**发送html的邮件*/
//    public void sendHTMLMail(MailBean mailBean) {
//        MimeMessage mimeMailMessage = null;
//        try {
//            mimeMailMessage = javaMailSender.createMimeMessage();
//            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
//            mimeMessageHelper.setFrom(MAIL_SENDER);
//            mimeMessageHelper.setTo(mailBean.getRecipient());
//            mimeMessageHelper.setSubject(mailBean.getSubject());
//            mimeMessageHelper.setText("<h1>hello</h1>", true);
//            javaMailSender.send(mimeMailMessage);
//        } catch (Exception e) {
//            log.error("邮件发送失败", e.getMessage());
//        }
//    }

    /**带附件格式的邮件*/
//    public void sendAttachmentMail(MailBean mailBean) {
//        MimeMessage mimeMailMessage = null;
//        try {
//            mimeMailMessage = javaMailSender.createMimeMessage();
//            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
//            mimeMessageHelper.setFrom(MAIL_SENDER);
//            mimeMessageHelper.setTo(mailBean.getRecipient());
//            mimeMessageHelper.setSubject(mailBean.getSubject());
//            mimeMessageHelper.setText(mailBean.getContent());
//            //文件路径  spring的FileSystemResource,使用绝对路径访问文件资源
//            FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/mail.png"));
//            mimeMessageHelper.addAttachment("mail.png", file);
//
//            javaMailSender.send(mimeMailMessage);
//        } catch (Exception e) {
//            log.error("邮件发送失败", e.getMessage());
//        }
//    }

    /**发送带静态资源的邮件*/
//    public void sendInlineMail(MailBean mailBean) {
//        MimeMessage mimeMailMessage = null;
//        try {
//            mimeMailMessage = javaMailSender.createMimeMessage();
//            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
//            mimeMessageHelper.setFrom(MAIL_SENDER);
//            mimeMessageHelper.setTo(mailBean.getRecipient());
//            mimeMessageHelper.setSubject(mailBean.getSubject());
//            mimeMessageHelper.setText("<html><body>带静态资源的邮件内容，这个一张IDEA配置的照片:<img src='cid:picture' /></body></html>", true);
//            //文件路径
//            FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/mail.png"));
//            mimeMessageHelper.addInline("picture", file);
//
//            javaMailSender.send(mimeMailMessage);
//        } catch (Exception e) {
//            log.error("邮件发送失败", e.getMessage());
//        }
//    }


    /**基于Freemarker的模板邮件*/
//    public void sendTemplateMail(MailBean mailBean) {
//        MimeMessage mimeMailMessage = null;
//        try {
//            mimeMailMessage = javaMailSender.createMimeMessage();
//            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
//            mimeMessageHelper.setFrom(MAIL_SENDER);
//            mimeMessageHelper.setTo(mailBean.getRecipient());
//            mimeMessageHelper.setSubject(mailBean.getSubject());
//
//            Map<String, Object> model = new HashMap<String, Object>();
//            model.put("content", mailBean.getContent());
//            model.put("title", "标题Mail中使用了FreeMarker");
//            Template template = configuration.getTemplate("mail.ftl");
//            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
//            mimeMessageHelper.setText(text, true);
//
//            javaMailSender.send(mimeMailMessage);
//        } catch (Exception e) {
//            log.error("邮件发送失败", e.getMessage());
//        }
//
//    }


    /**动态添加邮箱配置信息*/
    /*public void sendSimpleMail() throws Exception {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();//直接生产一个实例
        String users[] = {"xxxxx@qq.com","xxxxx@126.com"};
        mailSender.setHost("smtp.126.com");//动态添加配置
        mailSender.setPassword("xxxxx");
        mailSender.setPort(25);
        mailSender.setProtocol("smtp");
        mailSender.setUsername("xxxxx");
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("xxxxxxx");
        message.setTo(users); // 使用数组的形式还可以群发
        message.setSubject("羽毛球比赛");
        message.setText("报名参加羽毛球赛");
        mailSender.send(message);
    }*/
}
